package com.qf.p1.controller;

import com.alibaba.fastjson.JSONObject;
import com.qf.p1.controller.base.BaseController;
import com.qf.p1.dto.SignInDTO;
import com.qf.p1.model.CourseTname;
import com.qf.p1.model.User;
import com.qf.p1.result.R;
import com.qf.p1.service.UserService;
import com.qf.p1.util.JwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户的Controller
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.04.06
 * @since 1.0
 */
@RestController
@RequestMapping("Demo/user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    @PostMapping("signIn")
    public R signIn(@RequestBody SignInDTO signInDTO) {
        String signInName = signInDTO.getSignInName();
        String password = signInDTO.getPassword();
        //登陆
        User user = userService.signIn(signInName, password);
        if(user==null){
            //用户登录信息有误
            return R.error(501,"用户登录信息有误");
        }else {
            //签发token
            String token=JwtUtil.generatorToken(user);
            if (user.getRole()==0){
                //学生
                return R.ok(200,"学生登录成功")
                        .setResult("token",token)
                        .setResult("user",user);
            }else {
                //老师
                return R.ok(201,"教师登录成功")
                        .setResult("token",token)
                        .setResult("user",user);
            }
        }

    }

    @GetMapping("username")
    public R username(@RequestParam String username){
        boolean result = userService.findUserName(username);
        if (result){
            return R.error(500,"用户名已经存在");
        }else {
            return R.ok(200,"用户名可用");
        }
    }

    @PostMapping("signUp")
    public R signUp(@RequestBody JSONObject signUpDTO){
        String username=signUpDTO.getString("username");
        String password=signUpDTO.getString("password");
        String gender = signUpDTO.getString("gender");
        //注册
        userService.signUp(username,password,gender);
        return R.ok("注册成功");
    }

    @PostMapping("course")
    public R selectedCourse(@RequestBody JSONObject selectedCourseDTO){
        Integer cid = selectedCourseDTO.getInteger("cid");
        Integer userId=userId();
        boolean result = userService.selectCourse(userId, cid);
        if (result){
            return R.ok("选课成功");
        }
        return R.error("已经选满五门课程了");
    }
    @DeleteMapping("course")
    public R delete(@RequestBody JSONObject selectObject){
        Integer cid = selectObject.getInteger("cid");
        Integer uid = selectObject.getInteger("uid");
        if(uid==null){
            Integer userId=userId();
            userService.unSelectCourse(userId,cid);
        }else {
            userService.unSelectCourse(uid,cid);
        }
        return R.ok("删除选课成功");

    }
    @GetMapping("course")
    public R findCourses(){
        Integer userId=userId();
        List<CourseTname> courses = userService.findCourses(userId);
        return R.ok(200,"查询成功").setResult("courses",courses);

    }

}
